﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SCG.SolarVengeanceEngine;
using SCG.General;
using SCG.WinForms;

namespace SCG.SolarVengeance
{
    public partial class ConfigurationPage : BasePage
    {
        //constructors
        public ConfigurationPage()
            : base()
        {
            InitializeComponent();
        }

        //initialization
        public override void Initialize()
        {
            //name and glyph
            txtName.Text = MainForm.StarLordName;
            picGlyph.Image = MainForm.Glyph;
            pnlColor.BackColor = MainForm.PreferredColor.ToColor();

            //populate image list and listview with starship types
            if (lvShipTypes.Items.Count == 0)
            {
                lvShipTypes.AddAllStarShipTypes();
                foreach (StarShipType st in StarShip.ShipTypes)
                {
                    ListViewItem lvi = lvShipTypes.Items[(int)st];
                    lvi.Checked = MainForm.SelectedShipTypes.Contains(st);
                }
                lvShipTypes.Items[0].Selected = true;
            }
        }

        //save information in settings and return
        private void btnProceed_Click(object sender, EventArgs e)
        {
            //same name and glyph
            MainForm.StarLordName = txtName.Text;
            MainForm.Glyph = picGlyph.Image;
            MainForm.PreferredColor = pnlColor.BackColor.ToSCGColor();

            //save selected starships
            MainForm.SelectedShipTypes.Clear();
            for(int n = 0; n < lvShipTypes.Items.Count; n++)
                if (lvShipTypes.Items[n].Checked)
                {
                    StarShipType st = (StarShipType)n;
                    MainForm.SelectedShipTypes.Add(st);
                }

            MainForm.PersistStarLordSettings();
            MainForm.ShowPage(Pages.Title);
        }

        //click - select image
        private void picGlyph_Click(object sender, EventArgs e)
        {
            if (dlgSelectImage.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    Image img = Image.FromFile(dlgSelectImage.FileName);
                    img = ImageUtils.ResizeBitmap(img, 60, 60, true);
                    picGlyph.Image = img;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("There was a problem loading this image: " + dlgSelectImage.FileName + Environment.NewLine + ex.Message);
                }
            }
        }

        //cancel without saving
        private void btnCancel_Click(object sender, EventArgs e)
        {
            MainForm.ShowPage(Pages.Title);
        }

        //validation
        private void txtName_TextChanged(object sender, EventArgs e)
        {
            ValidateInput();
        }
        private void lvShipTypes_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            ValidateInput();
        }
        private void ValidateInput()
        {
            int n = 0;
            foreach (ListViewItem lvi in lvShipTypes.Items)
                if (lvi.Checked)
                    n++;
            btnProceed.Enabled = txtName.Text != "" && n == 6;
        }

        //selected a starshgip
        private void lvStarShips_StarShipTypeSelected(object sender, StarShipTypeEventArgs e)
        {
            int idx = (int)e.StarShipType;
            imgShip.Image = lvShipTypes.SmallImageList.Images[idx];
            StarShip ship = (StarShip)lvShipTypes.Items[idx].Tag;
            lblShipType.Text = ship.Name;
            lblDescription.Text = ship.Description;
        }

        //select color
        private void pnlColor_Click(object sender, EventArgs e)
        {
            if (dlgColor.ShowDialog(MainForm) == DialogResult.OK)
            {
                SCGColor color = dlgColor.Color.ToSCGColor();
                while (color.Luminance < 60)
                    color.Brighten();
                pnlColor.BackColor = color.ToColor();
            }
        }
    }
}
